﻿using System.Text;
using System.Text.RegularExpressions;
using Kyoh.App.Crawler.Plugin;
using Kyoh.Lib.Plugin;
using System;
using System.Runtime.InteropServices;

namespace Crawler.Parser.TwitPic
{
    [Guid("DC928DE7-A3C7-4642-86E6-33784CD4F38D")]
    public class TwitPicParser : TypicalParser
    {
        public TwitPicParser()
            : base(
                @"(?<uri>http://twitpic\.com/(?<contentId>\w+))",
                @"<img src=""http://[\w-]+\.twitpicproxy\.com/photos/full/\d+\.\w+\?key=\d+"" alt=""(?<title>[^""]+)"" style=""width:\d+px;"">",
                @"",
                @"<img src=""(?<content>http://[\w-]+\.twitpicproxy\.com/photos/full/\d+\.\w+\?key=\d+)"" alt=""[^""]+"" style=""width:\d+px;"">",
                Encoding.UTF8)
        {
        }

        protected override string ContentIdPrefix
        {
            get { return "twitpic"; }
        }

        protected override string GetExtension(string pageUri, string contentId, string title, string contentUri)
        {
            var match = Regex.Match(contentUri, @"<img src=""http://[\w-]+\.twitpicproxy\.com/photos/full/\d+\.(?<ext>\w+)\?key=\d+"" alt=""[^""]+"" style=""width:\d+px;"">");
            if (match.Success)
                return match.Groups["ext"].Value;
            return "jpg";
        }

        public override string Author { get { return "kyoh"; } }
        public override string Description { get { return string.Empty; } }
        public override Uri Uri { get { return new Uri("http://kyoh.net/"); } }
        public override string Name { get { return "TwitPic"; } }
    }
}
